/**
* All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
*
* MmsBaseConfigService
* 意见反馈基础配置表
*
* 修改纪录
* 2021-10-29 版本：1.0 zhangqianbo 创建。
* @version 版本：1.0
* @author 作者：zhangqianbo
* 创建日期：2021-10-29
*/
package com.topscomm.mms.service.impl;

import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.mms.pojo.MmsBaseConfigEntity;
import com.topscomm.mms.service.IMmsBaseConfigService;
import com.topscomm.mms.service.impl.auto.MmsBaseConfigServiceAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service(value = "mmsBaseConfigService")
@Scope(value = "singleton")
public class MmsBaseConfigService extends MmsBaseConfigServiceAuto implements IMmsBaseConfigService {

    /**
     * @Description: 判断流程文档相关的反馈配置时不能对相同的流程进行多个配置
     * @Author: mengmeng
     * @param: entity ->
     * @Return: void
     * @exception:
     * @Date: 2022/10/8 14:21
     * @Version: 1.0
     **/
    @Override
    protected void beforeInsert(BasicEntity entity) throws ServiceException {
        MmsBaseConfigEntity mmsBaseConfigEntity = (MmsBaseConfigEntity) entity;
        // 判断是流程文档相关的配置
        String belongBusiness = mmsBaseConfigEntity.getBelongbusiness();
        String feedBackType = mmsBaseConfigEntity.getFeedbacktype();
        if (StringUtil.isEmpty(belongBusiness) || StringUtil.isEmpty(feedBackType)) {
            return;
        }
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" {0} = '{1}' ", MmsBaseConfigEntity.FieldBelongbusiness, belongBusiness);
        List<MmsBaseConfigEntity> list = this.dao.queryByWhere(sql.toString());
        // 如果不空，则存在该流程的配置
        judgeExistSameConfig(feedBackType, list);
    }

    /**
     * @Description: 方法作用描述
     * @Author: mengmeng
     * @param: feedBackType -> 新的业务类型
     * @param: list -> 已存在的业务类型
     * @Return: void
     * @exception:
     * @Date: 2022/10/8 15:34
     * @Version: 1.0
     **/
    private void judgeExistSameConfig(String feedBackType, List<MmsBaseConfigEntity> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        // 具体判断该流程是否存在相同的反馈类型
        Set<String> feedBackTypeSet = new HashSet<>();
        for (MmsBaseConfigEntity configEntity : list) {
            String temp = configEntity.getFeedbacktype();
            if (!StringUtil.isEmpty(temp)) {
                feedBackTypeSet.addAll(Arrays.stream(temp.split(",")).collect(Collectors.toSet()));
            }
        }
        for (String s : feedBackType.split(",")) {
            if (feedBackTypeSet.contains(s)) {
                throw new ServiceException("已存在该流程的反馈配置信息！");
            }
        }
    }

    @Override
    protected void updateBefore(Map<String, Object> map) throws ServiceException {
        MmsBaseConfigEntity mmsBaseConfigEntity = new MmsBaseConfigEntity();
        mmsBaseConfigEntity.convertFromMap(map);
        // 判断是流程文档相关的配置
        String belongBusiness = mmsBaseConfigEntity.getBelongbusiness();
        String feedBackType = mmsBaseConfigEntity.getFeedbacktype();
        if (StringUtil.isEmpty(belongBusiness) || StringUtil.isEmpty(feedBackType)) {
            return;
        }
        // 判断是否存在该流程的配置(排除自己)
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" {0} = '{1}' ", MmsBaseConfigEntity.FieldBelongbusiness, belongBusiness);
        sql.appendSingle(" and {0} != {1} ", MmsBaseConfigEntity.FieldId, mmsBaseConfigEntity.getId());
        List<MmsBaseConfigEntity> list = this.dao.queryByWhere(sql.toString());
        // 如果不空，则存在该流程的配置
        judgeExistSameConfig(feedBackType, list);
    }
}
